package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Cstationtype;
import com.zr.model.Cuser;
import com.zr.service.InsertNewDisstationService;

/**
 * 
 * @author hc
 *
 */

@Controller
@Scope(value="prototype")
public class InsertNewDisstationAction extends ActionSupport implements ServletResponseAware,SessionAware{
	private HttpServletResponse resp;
	 private  Map<String,Object> session; 
	 private String disname;
	 private String distype;
	 private int uid;
	 private String disscope;
	 private Double disprice;
	 private int instation;
	 


	public String getDisname() {
		return disname;
	}

	public void setDisname(String disname) {
		this.disname = disname;
	}

	public String getDistype() {
		return distype;
	}

	public void setDistype(String distype) {
		this.distype = distype;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getDisscope() {
		return disscope;
	}

	public void setDisscope(String disscope) {
		this.disscope = disscope;
	}

	public Double getDisprice() {
		return disprice;
	}

	public void setDisprice(Double disprice) {
		this.disprice = disprice;
	}

	public int getInstation() {
		return instation;
	}

	public void setInstation(int instation) {
		this.instation = instation;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session=session;
		
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
       this.resp = response;		
	}
	
	@Resource
	private InsertNewDisstationService insertNewDisstationService;
	
	public void insertNewDisstation(){
		resp.setCharacterEncoding("utf8");
		Cuser u = (Cuser)session.get("cuser");
		int cuid = u.getUid();
		String cuname = u.getUname();
		Cstationtype cstationtype = insertNewDisstationService.getDistypeByInfo(distype);
			try {
				PrintWriter pw = resp.getWriter();
				if(insertNewDisstationService.getStataionExistsOrNot(disname)==1){
					pw.write("2");
					pw.flush();
					pw.close();
				}else{
					if("".equals(disname)){
						pw.write("3");
						pw.flush();
						pw.close();
					}else if("".equals(distype)){
						pw.write("4");
						pw.flush();
						pw.close();
					}else if("".equals(disscope)){
						pw.write("5");
						pw.flush();
						pw.close();
					}else{
						insertNewDisstationService.insertNewDisstation(cuname, cuid, disname, cstationtype, uid, disscope, disprice, instation);;			
						pw.write("1");
						pw.flush();
						pw.close();
					}
				}
				
				
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		 
		
		
		}
		
		
		
	}